<?php
// 应用公共文件 

/**
 * 去除所有空格
 *
 * @param string $str
 *
 * @return string
 */
function trimall($str)
{
    $qian = array(" ", "　", "\t", "\n", "\r");
    $hou = array("", "", "", "", "");
    return str_replace($qian, $hou, $str);
}

function random($length, $chars = '0123456789')
{
    $hash = '';
    $max = strlen($chars) - 1;
    for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}
function isMobile($mobile)
{
    return preg_match('#^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$#', $mobile) ? true : false;
}
function arrayColumn($input, $columnKey, $indexKey = null)
{
    if (!function_exists('array_column')) {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
        $result = array();
        foreach ((array)$input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    } else {
        return array_column($input, $columnKey, $indexKey);
    }
}
 
/**
 * 发送HTTP请求方法
 * @param  string $url    请求URL
 * @param  array  $params 请求参数
 * @param  string $method 请求方法GET/POST
 * @return array  $data   响应数据
 */
function httpSend($url, $params=array(), $method = 'GET', $header = array(), $multi = false){ 
    $opts = array(
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HTTPHEADER     => $header
    );
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
    case 'TGET':
      $opts[CURLOPT_URL] = $url;
      break;
        case 'POST':
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            E('不支持的请求方式！');
    }
    
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) exit('请求发生错误：' . $error);
    
    return  $data;
} 

function getClientIp(){
        $ip=false;  
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){  
            $ip = $_SERVER["HTTP_CLIENT_IP"];  
        }  
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);  
            if ($ip) {
                array_unshift($ips, $ip); $ip = FALSE;
            }
            for ($i = 0; $i < count($ips); $i++) {  
                if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {  
                    $ip = $ips[$i];  
                    break;  
                }  
            }  
        }  
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

function formatTime($atime, $format = 'Y-m-d')
{
    if (is_string($atime)) {
        $atime = strtotime($atime);
    }
    $time = time();
    $timeDiff = $time - $atime;
    if ($timeDiff < 60) {
        $formatTime = "1分钟前";
    } elseif ($timeDiff > 60 && $timeDiff < 3600) {
        $formatTime = floor($timeDiff / 60) . "分钟前";
    } elseif ($timeDiff > 3600 && $timeDiff < 3600 * 24) {
        $formatTime = floor($timeDiff / 3600) . "小时前";
    } elseif ($timeDiff > 3600 * 24 && $timeDiff < 3600 * 24 * 30) {
        $formatTime = floor($timeDiff / (3600 * 24)) . "天前";
    } else {
        $formatTime = date($format, $atime);
    }
    return $formatTime;
}


/**
 *   实现中文字串截取无乱码的方法
 */
function getSubstr($string, $start, $length, $fuhao = "...")
{
    if (mb_strlen($string, 'utf-8') > $length) {
        $str = mb_substr($string, $start, $length, 'utf-8');
        return $str . $fuhao;
    } else {
        return $string;
    }

}
function getTree($array)
{
    $items = array();
    foreach ($array as $value) {
        $items[$value['id']] = $value;
        if ($value['parent_id']==0){
            $items[$value['id']]['son']='';
        }
    }
//    sort($items);
    //第二部 遍历数据 生成树状结构
    $tree = array();


    foreach ($items as $key => $item) {
        $items[$key]['time']=formatTime($item['create_time']);
        if (isset($items[$item['parent_id']])) {
            $items[$item['parent_id']]['son'][] = &$items[$key];
        } else {
//            $items[$key]['son']='';
            $tree[] = &$items[$key];
        }
    }
    return $tree;

}

function new_addslashes($string){
    if(!is_array($string)) return addslashes($string);
    foreach($string as $key => $val) $string[$key] = new_addslashes($val);
    return $string;
}
function new_stripslashes($string){
    if(!is_array($string)) return stripslashes($string);
    foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
    return $string;
}
//生成 验证码
function create_code($length = 6 , $numeric = 1) {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
}
//xml转数组
function xml_to_array($xml){
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches)){
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )){
                $arr[$key] =xml_to_array( $subxml );
            }else{
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}
